package com.dtranx.tools.corpora.ability.search;

import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import com.dtranx.tools.corpora.ability.enums.SearchType;
import com.dtranx.tools.corpora.ability.vo.MatchingVo;
import com.dtranx.tools.corpora.abilityapi.enums.SearchMode;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @ClassName TermModeSearch
 * @Description TODO
 * @Date 2024/3/12 16:39
 * @Author ly
 **/
@Slf4j
public class TermModeSearch extends AbstractModeSearch{

    @Override
    public void buildQueryBuilder0(BoolQuery.Builder queryBuilder, MatchingVo matchingVo) {
        queryBuilder.must(Query.of(mustCase->{
            mustCase.term(termBuilder->{
                termBuilder.field(matchingVo.isReverse() ? SearchType.KEYWORD_TYPE.getTranslationKey() : SearchType.KEYWORD_TYPE.getOriginalKey());
                termBuilder.value(matchingVo.getValue());
                return termBuilder;
            });
            if (StringUtils.isNotEmpty(matchingVo.getCompressValue())) {
                mustCase.term(termBuilder->{
                    termBuilder.field(matchingVo.isReverse() ? SearchType.COMPRESS_TYPE.getTranslationKey() : SearchType.COMPRESS_TYPE.getOriginalKey());
                    termBuilder.value(matchingVo.getCompressValue());
                    return termBuilder;
                });
            }
            return mustCase;
        }));
    }

    /**
     * 分词后，需要精准匹配
     * @param queryBuilder
     * @param matchingVo
     */
    @Override
    public void buildAnalyzeQueryBuilder0(BoolQuery.Builder queryBuilder, MatchingVo matchingVo) {
        List<String> tempValues = Lists.newArrayList(matchingVo.getValue());
        try {
            Set<String> noRepeatInfos = Sets.newHashSet();
            //Es分词结果
            noRepeatInfos.addAll(matchingVo.getModeService().analyze(matchingVo.isMinor(), matchingVo.getValue()));
            tempValues.addAll( Lists.newArrayList(noRepeatInfos));
        } catch (IOException e) {
            log.error("分词失败:",e);
            super.buildAnalyzeQueryBuilder0(queryBuilder, matchingVo);
            return;
        }
        List<String> values = tempValues;
        queryBuilder.must(Query.of(mustCase->{
            mustCase.terms(termBuilder->{
                termBuilder.field(matchingVo.isReverse() ? SearchType.KEYWORD_TYPE.getTranslationKey() : SearchType.KEYWORD_TYPE.getOriginalKey());
                termBuilder.terms(builder -> {
                    builder.value(values.stream().map(FieldValue::of).collect(Collectors.toList()));
                    return builder;
                });
                return termBuilder;
            });
            return mustCase;
        }));
    }

    @Override
    public SearchMode getMode() {
        return SearchMode.TERM;
    }
}
